<!DOCTYPE html>
<html>
<!--
Copyright 2013 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.

////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
//                                                                           //
// Any edits to this file must be applied to vec3f_test.html by running:     //
//   swap_type.sh vec3d_test.html > vec3f_test.html                          //
//                                                                           //
////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////

-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.vec.vec3d</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.vec.Float64Array');
  goog.require('goog.vec.vec3d');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

  function testCreate() {
    var v = goog.vec.vec3d.create();
    assertElementsEquals([0, 0, 0], v);
  }

  function testSet() {
    var v = goog.vec.vec3d.create();
    goog.vec.vec3d.setFromValues(v, 1, 2, 3);
    assertElementsEquals([1, 2, 3], v);

    goog.vec.vec3d.setFromArray(v, [4, 5, 6]);
    assertElementsEquals([4, 5, 6], v);

    var w = goog.vec.vec3d.create();
    goog.vec.vec3d.setFromValues(w, 1, 2, 3);
    assertElementsEquals([1, 2, 3], w);

    goog.vec.vec3d.setFromArray(w, [4, 5, 6]);
    assertElementsEquals([4, 5, 6], w);
  }

  function testAdd() {
    var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
    var v1 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [4, 5, 6]);
    var v2 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);

    goog.vec.vec3d.add(v2, v1, v2);
    assertElementsEquals([1, 2, 3], v0);
    assertElementsEquals([4, 5, 6], v1);
    assertElementsEquals([5, 7, 9], v2);

    goog.vec.vec3d.add(goog.vec.vec3d.add(v0, v1, v2), v0, v2);
    assertElementsEquals([6, 9, 12], v2);
  }

  function testSubtract() {
    var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
    var v1 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [4, 5, 6]);
    var v2 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);

    goog.vec.vec3d.subtract(v2, v1, v2);
    assertElementsEquals([1, 2, 3], v0);
    assertElementsEquals([4, 5, 6], v1);
    assertElementsEquals([-3, -3, -3], v2);

    goog.vec.vec3d.setFromValues(v2, 0, 0, 0);
    goog.vec.vec3d.subtract(v1, v0, v2);
    assertElementsEquals([3, 3, 3], v2);

    v2 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
    goog.vec.vec3d.subtract(v2, v1, v2);
    assertElementsEquals([-3, -3, -3], v2);

    goog.vec.vec3d.subtract(goog.vec.vec3d.subtract(v1, v0, v2), v0, v2);
    assertElementsEquals([2, 1, 0], v2);
  }

  function testNegate() {
    var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
    var v1 = goog.vec.vec3d.create();

    goog.vec.vec3d.negate(v0, v1);
    assertElementsEquals([-1, -2, -3], v1);
    assertElementsEquals([1, 2, 3], v0);

    goog.vec.vec3d.negate(v0, v0);
    assertElementsEquals([-1, -2, -3], v0);
  }

  function testAbs() {
    var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [-1, -2, -3]);
    var v1 = goog.vec.vec3d.create();

    goog.vec.vec3d.abs(v0, v1);
    assertElementsEquals([1, 2, 3], v1);
    assertElementsEquals([-1, -2, -3], v0);

    goog.vec.vec3d.abs(v0, v0);
    assertElementsEquals([1, 2, 3], v0);
  }

  function testScale() {
    var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
    var v1 = goog.vec.vec3d.create();

    goog.vec.vec3d.scale(v0, 4, v1);
    assertElementsEquals([4, 8, 12], v1);
    assertElementsEquals([1, 2, 3], v0);

    goog.vec.vec3d.setFromArray(v1, v0);
    goog.vec.vec3d.scale(v1, 5, v1);
    assertElementsEquals([5, 10, 15], v1);
  }

  function testMagnitudeSquared() {
    var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
    assertEquals(14, goog.vec.vec3d.magnitudeSquared(v0));
  }

  function testMagnitude() {
    var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
    assertEquals(Math.sqrt(14), goog.vec.vec3d.magnitude(v0));
  }

  function testNormalize() {
    var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [2, 3, 4]);
    var v1 = goog.vec.vec3d.create();
    var v2 = goog.vec.vec3d.create();
    goog.vec.vec3d.scale(
        v0, 1 / goog.vec.vec3d.magnitude(v0), v2);

    goog.vec.vec3d.normalize(v0, v1);
    assertElementsEquals(v2, v1);
    assertElementsEquals([2, 3, 4], v0);

    goog.vec.vec3d.setFromArray(v1, v0);
    goog.vec.vec3d.normalize(v1, v1);
    assertElementsEquals(v2, v1);
  }

  function testDot() {
    var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
    var v1 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [4, 5, 6]);
    assertEquals(32, goog.vec.vec3d.dot(v0, v1));
    assertEquals(32, goog.vec.vec3d.dot(v1, v0));
  }

  function testCross() {
    var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
    var v1 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [4, 5, 6]);
    var crossVec = goog.vec.vec3d.create();

    goog.vec.vec3d.cross(v0, v1, crossVec);
    assertElementsEquals([1, 2, 3], v0);
    assertElementsEquals([4, 5, 6], v1);
    assertElementsEquals([-3, 6, -3], crossVec);

    goog.vec.vec3d.setFromArray(crossVec, v1);
    goog.vec.vec3d.cross(crossVec, v0, crossVec);
    assertElementsEquals([1, 2, 3], v0);
    assertElementsEquals([4, 5, 6], v1);
    assertElementsEquals([3, -6, 3], crossVec);

    goog.vec.vec3d.cross(v0, v0, v0);
    assertElementsEquals([0, 0, 0], v0);
  }

  function testDistanceSquared() {
    var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
    var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
    assertEquals(0, goog.vec.vec3d.distanceSquared(v0, v1));
    goog.vec.vec3d.setFromValues(v0, 1, 2, 3);
    goog.vec.vec3d.setFromValues(v1, -1, -2, -1);
    assertEquals(36, goog.vec.vec3d.distanceSquared(v0, v1));
  }

  function testDistance() {
    var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
    var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
    assertEquals(0, goog.vec.vec3d.distance(v0, v1));
    goog.vec.vec3d.setFromValues(v0, 1, 2, 3);
    goog.vec.vec3d.setFromValues(v1, -1, -2, -1);
    assertEquals(6, goog.vec.vec3d.distance(v0, v1));
  }

  function testDirection() {
    var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
    var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
    var dirVec = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 4, 5, 6);
    goog.vec.vec3d.direction(v0, v1, dirVec);
    assertElementsEquals([0, 0, 0], dirVec);
    goog.vec.vec3d.setFromValues(v0, 0, 0, 0);
    goog.vec.vec3d.setFromValues(v1, 1, 0, 0);
    goog.vec.vec3d.direction(v0, v1, dirVec);
    assertElementsEquals([1, 0, 0], dirVec);
    goog.vec.vec3d.setFromValues(v0, 1, 1, 1);
    goog.vec.vec3d.setFromValues(v1, 0, 0, 0);
    goog.vec.vec3d.direction(v0, v1, dirVec);
    assertElementsRoughlyEqual(
        [-0.5773502588272095, -0.5773502588272095, -0.5773502588272095],
        dirVec, goog.vec.EPSILON);
  }

  function testLerp() {
    var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
    var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 10, 20, 30);
    var v2 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);

    goog.vec.vec3d.lerp(v2, v1, 0, v2);
    assertElementsEquals([1, 2, 3], v2);
    goog.vec.vec3d.lerp(v2, v1, 1, v2);
    assertElementsEquals([10, 20, 30], v2);
    goog.vec.vec3d.lerp(v0, v1, .5, v2);
    assertElementsEquals([5.5, 11, 16.5], v2);
  }

  function testMax() {
    var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 10, 20, 30);
    var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 5, 25, 35);
    var v2 = goog.vec.vec3d.create();

    goog.vec.vec3d.max(v0, v1, v2);
    assertElementsEquals([10, 25, 35], v2);
    goog.vec.vec3d.max(v1, v0, v1);
    assertElementsEquals([10, 25, 35], v1);
    goog.vec.vec3d.max(v2, 20, v2);
    assertElementsEquals([20, 25, 35], v2);
  }

  function testMin() {
    var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 10, 20, 30);
    var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 5, 25, 35);
    var v2 = goog.vec.vec3d.create();

    goog.vec.vec3d.min(v0, v1, v2);
    assertElementsEquals([5, 20, 30], v2);
    goog.vec.vec3d.min(v1, v0, v1);
    assertElementsEquals([5, 20, 30], v1);
    goog.vec.vec3d.min(v2, 20, v2);
    assertElementsEquals([5, 20, 20], v2);
  }

  function testEquals() {
    var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
    var v1 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
    assertElementsEquals(v0, v1);

    v1[0] = 4;
    assertFalse(goog.vec.vec3d.equals(v0, v1));

    v1 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
    v1[1] = 4;
    assertFalse(goog.vec.vec3d.equals(v0, v1));

    v1 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
    v1[2] = 4;
    assertFalse(goog.vec.vec3d.equals(v0, v1));
  }

</script>
</body>
